/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service.system.impl;

import dao.system.SystemGroupAccessDao;
import dao.system.SystemMenuDao;
import java.util.List;
import javax.net.ssl.SSLContext;
import model.system.Policy;
import model.system.SystemGroupAccess;
import model.system.SystemMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.system.SystemGroupAccessService;
import utility.Constants;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
@Service("systemGroupAccessService")
@Transactional(readOnly=true)
public class SystemGroupAccessImpl implements SystemGroupAccessService {
    @Autowired SystemGroupAccessDao systemGroupAccessDao;    
    @Autowired SystemMenuDao systemMenuDao;    
    @Transactional(readOnly=false)

    @Override
    public boolean save(List<SystemGroupAccess> systemGroupAccess) {
        boolean rs = false;
        for (SystemGroupAccess group : systemGroupAccess){
            systemGroupAccessDao.save(group);
        }
        return rs;
    }

    @Override
    public boolean delete(SystemGroupAccess systemGroupAccess) {
        return systemGroupAccessDao.delete(systemGroupAccess);
    }

    @Override
    public SystemGroupAccess getSystemGroupAccess(int id) {
        return systemGroupAccessDao.getById(id);
    }

    @Override
    public SystemGroupAccess getSystemGroupAccess(String kode) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<SystemGroupAccess> getSystemGroupAccess() {
        return systemGroupAccessDao.getAll("from SystemGroupAccess as gp");// ,SystemMenu as menu,SystemGroup as group "
                //+ "where menu.menuId = gp.menuId and group.group_id= gp.group_id");
        
    }

    @Override
    public List<SystemGroupAccess> getSystemGroupAccess(int start, int num) {
        return systemGroupAccessDao.getAll(start, num);
    }

    @Override
    public List<SystemGroupAccess> getAll(int start, int num, String params, String orderBy) {
        return systemGroupAccessDao.getAll(start, num, params, orderBy);
    }

    @Override
    public List<SystemMenu> getSystemMenu() {
        return systemMenuDao.getAll();
    }

    @Override
        public SystemGroupAccess getAccessByMenuAndGroupId(int menuId, int groupId) {
        String param = " x.id.menuId = "+menuId+" and x.id.groupId="+groupId;
        
        return systemGroupAccessDao.getByParam(param);
    }

    @Override
    public Policy allowAccess(int menuId, int groupId) {
        String grpPolicy = "";
        SystemGroupAccess grp = getAccessByMenuAndGroupId(menuId, groupId);
        if (grp!=null) grpPolicy = grp.getPolicy();
        Policy policy = new Policy();
        policy.setCanAdd(grpPolicy.indexOf(Constants.POLICY_ADD)>-1);
        policy.setCanEdit(grpPolicy.indexOf(Constants.POLICY_EDIT)>-1);
        policy.setCanDelete(grpPolicy.indexOf(Constants.POLICY_DELETE)>-1);
        policy.setCanPrint(grpPolicy.indexOf(Constants.POLICY_PRINT)>-1);
        policy.setCanApprove(grpPolicy.indexOf(Constants.POLICY_APPROVAL)>-1);
        policy.setCanOpen(grpPolicy.indexOf(Constants.POLICY_OPEN)>-1);
        policy.setCanUnApprove(grpPolicy.indexOf(Constants.POLICY_UNAPPROVAL)>-1);
        return policy;
    }

    
}
